package com.xinxin.blockchain.common.handler;

import com.xinxin.base.entity.ApiResult;
import com.xinxin.base.exception.BusinessException;
import com.xinxin.base.handler.ValidExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler extends ValidExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public ApiResult handleException(Exception e) {
        log.error("系统内部异常", e);
        return ApiResult.fail("系统内部异常");
    }

    @ExceptionHandler(value = RuntimeException.class)
    public ApiResult handleRuntimeException(RuntimeException e) {
        log.error("系统错误", e);
        return ApiResult.fail(e.getMessage());
    }

    @ExceptionHandler(value = BusinessException.class)
    public ApiResult handleBusinessException(BusinessException e) {
        log.error("系统错误", e);
        return ApiResult.code(e.getCode()).msg(e.getMessage());
    }

    @ExceptionHandler(value = DataAccessException.class)
    public ApiResult handleDataAccessException(DataAccessException e) {
        log.error("数据库错误", e);
        return ApiResult.fail("数据库错误");
    }
}
